package com.zimmur.service.impl.account;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zimmur.common.EnumPointRule;
import com.zimmur.dao.account.IPointLogDao;
import com.zimmur.model.account.PointLog;
import com.zimmur.model.account.PointLogParameter;
import com.zimmur.model.account.UserMessageParameter;
import com.zimmur.model.account.view.LogView;
import com.zimmur.model.base.Systemconfig;
import com.zimmur.service.IAccountInfoService;
import com.zimmur.service.account.IPointLogService;
import com.zimmur.service.base.ISystemconfigService;
import com.zimmur.tools.DateUtils;

@Service("pointLogService")
public class PointLogServiceImpl implements IPointLogService{
	private static Logger log = LoggerFactory.getLogger(PointLogServiceImpl.class);
	
	@Resource
	private ISystemconfigService systemConfigService;
	@Resource
	private IPointLogDao pointLogDao;
	
	@Resource
	private IAccountInfoService accountInfoService;

	@Override
	public UserMessageParameter selectByPage(long userId, int curPage, int pageSize) {
		return null;
	}

	@Override
	public boolean mainPointLog(Long userId, String remark, EnumPointRule rule,int point) {
		if(userId>0 && !remark.equals("")){
			Systemconfig config = null;
			PointLog pointLog = new PointLog();
			pointLog.setUserId(userId);
			pointLog.setEventDate(DateUtils.getNowUnixTime());
			pointLog.setConfigValue(rule.getConfigName());
			pointLog.setRemark(remark);
			
			config = systemConfigService.getDefaultConfigValue(rule.getConfigName());
			if(config == null){
				return false;
			}
			
			Integer configValue = Integer.parseInt(config.getConfigValue());
			pointLog.setPoint(configValue);
			
			//首次注册或积分抵扣
			if(rule == EnumPointRule.ZK_REGISTER || rule == EnumPointRule.BUY_POINT_TO_JE){
				try{					
					config = systemConfigService.getDefaultConfigValue(rule.getConfigName());
					if(rule == EnumPointRule.ZK_REGISTER ){
						pointLog.setEvent("+");						
					}
					if(rule == EnumPointRule.BUY_POINT_TO_JE ){
						pointLog.setEvent("-");
						pointLog.setPoint(point*-1);
						
					}
					if(pointLogDao.insertSelective(pointLog)>0){
						accountInfoService.updateCurPointByUserId(userId, pointLog.getPoint(), remark);
					}
					return true;
				}catch(Exception er){
					log.error(er.getMessage());
				}
			}else{
				Integer limitValue = config.getLimitValue();
				String limitMethod = config.getLimitMethod();
				Integer sumCurPoint = 0;
				if(limitValue>0 && !limitMethod.equals("")){
					PointLogParameter parameter = new PointLogParameter();
					parameter.setUserId(userId);
					switch (limitMethod.toLowerCase()) {
					case "day":
						parameter.setStartTime(DateUtils.getCurDayStartToUnixTime());
						parameter.setEndTime(DateUtils.getCurDayEndToUnixTime());
						break;
					case "month":
						parameter.setStartTime(DateUtils.getMonthFirstDayToUnixTime());
						parameter.setEndTime(DateUtils.getMonthLastDayToUnixTime());
						break;
					case "year":
						parameter.setStartTime(DateUtils.getCurYearFirstDayToUnixTime());
						parameter.setEndTime(DateUtils.getCurYearLastDayToUnixTime());
						break;
					}
					sumCurPoint = pointLogDao.selectCountPointByTimeRange(parameter);
					if(sumCurPoint == null)
						sumCurPoint = 0;
				}
				try{
					if(sumCurPoint<limitValue){
						pointLog.setEvent("+");
						if(pointLogDao.insertSelective(pointLog)>0){
							accountInfoService.updateCurPointByUserId(userId, pointLog.getPoint(), remark);
						}
						return true;
					}
					
				}catch(Exception er){
					log.error(er.getMessage());
				}
			}
		}
		return false;
	}

	@Override
	public List<PointLog> selectPageListByUserId(PointLogParameter parameter) {
		return pointLogDao.selectPageListByUserId(parameter);
	}

	@Override
	public int selectPageListByUserIdCount(PointLogParameter parameter) {
		return pointLogDao.selectPageListCount(parameter);
	}
	
}
